<template>
	<view class="content">
		<view class="hello">
			<view class="title" v-if="hasLogin">
				您好 {{token}}，您已成功登录。
			</view>
			<view class="title" v-if="!hasLogin">
				您好，游客
			</view>
			<view class="ul">
				<button type="warn" @tap="qr()">扫一扫登录</button>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		mapState
	} from 'vuex'

	export default {
		computed: mapState(['forcedLogin', 'hasLogin', 'token']),
		methods:{
			qr(){
				console.log(this.token);
				if (!this.hasLogin) {
					uni.showModal({
						title: '未登录',
						content: '您未登录，需要登录后才能继续',
						showCancel:false,
						success: (res) => {
							if (res.confirm) {
								/**
								 * 如果需要强制登录，使用reLaunch方式
								 */
								if (this.forcedLogin) {
									uni.reLaunch({
										url: '../login/login'
									});
								} else {
									uni.navigateTo({
										url: '../login/login'
									});
								}
							}
						}
					});
				}else{
					let This = this;
					uni.scanCode({
					    success: function (res) {
					        let result = res.result;
							// 请求后台通知登录成功
							uni.request({
								url:"http://387d4s.natappfree.cc/user/code/"+result+"/login/"+This.token,
								method:"GET",
								success: (res) => {
									uni.showToast({
										title:res.data.message,
										icon:"none"
									});
								}
							})
					    }
					});
				}
			}
		}
	}
</script>

<style>
	.hello {
		display: flex;
		flex: 1;
		flex-direction: column;
	}

	.title {
		color: #8f8f94;
		margin-top: 25px;
	}

	.ul {
		font-size: 15px;
		color: #8f8f94;
		margin-top: 25px;
	}

	.ul>view {
		line-height: 25px;
	}
</style>
